home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Languages / Masm V6.11 / INCLUDE / PROLOGUE.IN$ / PROLOGUE
Encoding:
Text File  |  1992-10-01  |  9.0 KB  |  346 lines

  1. pushcontext listing
  2. .nolist
  3. ;
  4. ;       (C) Copyright Microsoft Corporation 1992
  5.  
  6. ;       This file is used to create the same sets of prologue and epilogue
  7. ;       sequences which the Microsoft C 6.00 compiler will produce.  This
  8. ;       file would be used for writing windows programs and to provide
  9. ;       such features as stack checking in the assembler portions of
  10. ;       a C based project.
  11.  
  12.  
  13. ;       The following global variables will affect the prolog/epilog
  14. ;       sequences produced
  15. ;
  16. ;       ?WP_DEBUG - If 1 then prolog/epilog sequences will be forced
  17. ;       ?WP_CHECKSTACK - If 1 then a check stack will be forced on all
  18. ;                       procedures
  19. ;       ?WP_INCBP - If 1 then the inc bp sequence will be generated on
  20. ;               all far procedures
  21. ;       ?WP_LOADDS - If 1 then the load ds sequence will be generated on
  22. ;               all far procedures
  23. ;
  24. ifndef ?WP_DEBUG
  25. ?WP_DEBUG = 0
  26. endif
  27. ifndef ?WP_CHECKSTACK
  28. ?WP_CHECKSTACK = 0
  29. endif
  30. ifndef ?WP_INCBP
  31. ?WP_INCBP = 0
  32. endif
  33. ifndef ?WP_LOADDS
  34. ?WP_LOADDS = 0
  35. endif
  36.  
  37. ;
  38. ;       Complain if we are in a segment as this will affect how the
  39. ;       externdefs are done and therefore the fixups and code
  40. ;       created on the checkstack calls
  41. ;
  42. % ifnb   <@CurSeg>
  43. echo    Include should not be contained in a segment
  44. endif
  45.  
  46. externdef C     _aNchkstk:near          ; Extern the symbols
  47. externdef C     _aFchkstk:far           ; for later reference
  48.  
  49. ;
  50. ;       This macro will produce the same output as will the 
  51. ;       C6 compiler for the given switches.
  52. ;
  53. ;       The following may be placed in the MacroArgs field of the
  54. ;               proc defintion:
  55. ;
  56. ;       CHECKSTACK
  57. ;       NOCHECKSTACK
  58. ;       LOADDS
  59. ;       NOLOADDS
  60. ;       FORCEFRAME
  61. ;       INCBP
  62. ;       NOINCBP
  63.  
  64. option prologue:cPrologue
  65.  
  66. cPrologue macro szProcName, flags, cbParams, cbLocals, rgRegs, rgUserParams
  67.     LOCAL   ?doPrologue
  68.     LOCAL   ?loadds
  69.     LOCAL   ?checkstack
  70.     LOCAL   ?incbp
  71.     LOCAL   ?cbLocals
  72. ;       pushcontext     listing
  73. ;       .nolistmacro
  74. ;       .listmacroall
  75.     
  76.     ?doPrologue = 0
  77.     ?loadds = 0
  78.     ?checkstack = 0
  79.     ?incbp = 0
  80.     ?cbLocals = cbLocals
  81.  
  82. ;;      Set the defaults based on the global values specified
  83. ;;
  84. if      ?WP_DEBUG NE 0                  ;; Force frames by default
  85.  ?doPrologue = 1
  86. endif
  87.  
  88. if      ?WP_CHECKSTACK NE 0             ;; Force checkstack by default
  89.  ?checkstack = 1
  90. endif
  91.  
  92. if      ?WP_INCBP NE 0                  ;; Force incbp by default if far
  93.  if flags AND 020h
  94.   ?incbp = 1
  95.  endif
  96. endif
  97.  
  98. if      ?WP_LOADDS NE 0                 ;; Force loadds by default if far
  99.  if flags AND 020h
  100.   ?loadds = 1
  101.  endif
  102. endif
  103.  
  104. ;;
  105. ;;      Get all of the user parameters parsed
  106. ;;
  107.  
  108. ifnb    <rgUserParams>          ;;      Parse user params if exsisting
  109.  for p,<rgUserParams>           ;;      For every user param
  110.  
  111.   ifidn <p>, <CHECKSTACK>       ;;      Is it checkstack?
  112.    ?checkstack = 1              ;;              Yes -- do checkstack
  113.   endif
  114.  
  115.   ifidn <p>, <NOCHECKSTACK>     ;;      Don't do checkstack?
  116.    ?checkstack = 0              ;;              Yes -- clear checkstack 
  117.   endif
  118.  
  119.   ifidn <p>, <LOADDS>   ;;      Is it LoadDS
  120.    ?loadds = 1                  ;;              Yes -- do loadds sequence
  121.   endif
  122.  
  123.   ifidn <p>, <NOLOADDS> ;;      Don't do LoadDS?
  124.    ?loadds = 0                  ;;              Yes -- clear loadds flag
  125.   endif
  126.  
  127.   ifidn <p>, <INCBP>            ;; Is it IncBP
  128.    if flags AND 020h            ;;      and far?        
  129.     ?incbp = 1                  ;;      Yes -- do IncBP sequence
  130.    endif
  131.   endif
  132.  
  133.   ifidn <p>, <NOINCBP>  ;;      Is it NoIncBP
  134.    ?incbp = 0                   ;;              Yes -- Clear the incbp flag
  135.   endif
  136.  
  137.   ifidn <p>, <FORCEFRAME>       ;;      Is it ForceFrame?
  138.    ?doPrologue = 1                      ;;              Yes -- force out a frame
  139.   endif
  140.  
  141.  endm                   ;; End of user parameter parsing loop
  142. endif
  143.  
  144. ;;  Frames are generated iff
  145. ;;      1. cbLocals + cbParams != 0
  146. ;;      2. FORCEFRAME is set
  147. ;;      3. INCBP is set and proc is far
  148. ;;      4. LOADDS is set
  149. ;;
  150. ;; Force a prolog?      
  151.  
  152. ?doPrologue = ?doPrologue OR ?incbp OR ?loadds OR ?checkstack OR (?cbLocals NE 0) OR (cbParams NE 0)
  153.  
  154. if      ?doPrologue EQ 0        ;; No prolog needed -- so get out of here
  155. ;       popcontext listing
  156.     exitm<0>
  157. endif
  158.  
  159. if      ?loadds EQ 1            ;; Create the loadds code -- force in
  160.     push    ds              ;;      Put DS into AX -- we will place
  161.     pop     ax              ;;      back in DS later.  This sequence
  162.     nop                     ;;      is altered by the OS if needed
  163. endif
  164.  
  165. if      ?incbp EQ 1             ;; Mark as a far procedure for stack
  166.     inc     bp              ;;      walking
  167. endif
  168.  
  169.     push    bp              ;; Create the frame
  170.     mov     bp,sp
  171.  
  172. if      ?loadds EQ 1            ;; Load up DS with the value in AX
  173.     push    ds              ;;
  174.     mov     ds,ax           ;;
  175.     ?cbLocals = ?cbLocals + 2
  176. endif
  177.  
  178. if      ?checkstack EQ 1        ;; Now  allocate space for locals
  179.     mov     ax,cbLocals     ;;      # of bytes of locals (unadjusted)
  180. % ifidn <@CurSeg>, <_TEXT>
  181.     call    _aNchkstk       ;;      Call run time routine to allocate
  182.  else
  183.     call    _aFchkstk
  184.  endif
  185. else    ; ?checkstack NE 1
  186.   if    cbLocals NE 0
  187.     sub     sp,cbLocals     ;;      make space on the stack for locals
  188.   endif
  189. endif
  190.  
  191. ifnb    rgRegs                  ;; There are registers to be saved.  do so
  192.     for r,rgRegs
  193.         push    r
  194.     endm
  195. endif
  196. ;       popcontext listing
  197.     exitm <?cbLocals>
  198.  
  199. endm
  200.  
  201.  
  202.  
  203. ;
  204. ;       This macro will produce the same output as will the 
  205. ;       C6 compiler for the given switches.
  206. ;
  207. ;       The following may be placed in the MacroArgs field of the
  208. ;               proc defintion:
  209. ;
  210. ;       CHECKSTACK
  211. ;       NOCHECKSTACK
  212. ;       LOADDS
  213. ;       NOLOADDS
  214. ;       FORCEFRAME
  215. ;       INCBP
  216. ;       NOINCBP
  217.  
  218. option epilogue:cEpilogue
  219.  
  220. cEpilogue macro szProcName, flags, cbParams, cbLocals, rgRegs, rgUserParams
  221.     LOCAL   ?doPrologue
  222.     LOCAL   ?loadds
  223.     LOCAL   ?checkstack
  224.     LOCAL   ?incbp
  225. ;       pushcontext     listing
  226. ;       .nolistmacro
  227. ;       .listmacroall
  228.     
  229.     ?doPrologue = 0
  230.     ?loadds = 0
  231.     ?checkstack = 0
  232.     ?incbp = 0
  233.  
  234. ;;      Set the defaults based on the global values specified
  235. ;;
  236. if      ?WP_DEBUG NE 0                  ;; Force frames by default
  237.  ?doPrologue = 1
  238. endif
  239.  
  240. if      ?WP_CHECKSTACK NE 0             ;; Force checkstack by default
  241.  ?checkstack = 1
  242. endif
  243.  
  244. if      ?WP_INCBP NE 0                  ;; Force incbp by default
  245.  if flags AND 020h
  246.   ?incbp = 1
  247.  endif
  248. endif
  249.  
  250. if      ?WP_LOADDS NE 0                 ;; Force loadds by default
  251.  if flags AND 020h
  252.   ?loadds = 1
  253.  endif
  254. endif
  255.  
  256. ;;
  257. ;;      Get all of the user parameters parsed
  258. ;;
  259.  
  260. ifnb    <rgUserParams>          ;;      Parse user params if exsisting
  261.  for p,<rgUserParams>           ;;      For every user param
  262.  
  263.   ifidn <p>, <CHECKSTACK>       ;;      Is it checkstack?
  264.    ?checkstack = 1              ;;              Yes -- do checkstack
  265.   endif
  266.  
  267.   ifidn <p>, <NOCHECKSTACK>     ;;      Don't do checkstack?
  268.    ?checkstack = 0              ;;              Yes -- clear checkstack 
  269.   endif
  270.  
  271.   ifidn <p>, <LOADDS>   ;;      Is it LoadDS
  272.    ?loadds = 1                  ;;              Yes -- do loadds sequence
  273.   endif
  274.  
  275.   ifidn <p>, <NOLOADDS> ;;      Don't do LoadDS?
  276.    ?loadds = 0                  ;;              Yes -- clear loadds flag
  277.   endif
  278.  
  279.   ifidn <p>, <INCBP>    ;;      Is it IncBP
  280.    if flags AND 020h
  281.     ?incbp = 1                  ;;              Yes -- do IncBP sequence
  282.    endif
  283.   endif
  284.  
  285.   ifidn <p>, <NOINCBP>  ;;      Is it NoIncBP
  286.    ?incbp = 0                   ;;              Yes -- Clear the incbp flag
  287.   endif
  288.  
  289.   ifidn <p>, <FORCEFRAME>       ;;      Is it ForceFrame?
  290.    ?doPrologue = 1                      ;;              Yes -- force out a frame
  291.   endif
  292.  
  293.  endm                   ;; End of user parameter parsing loop
  294. endif
  295.  
  296. ;;  Frames are generated iff
  297. ;;      1. cbLocals + cbParams != 0
  298. ;;      2. FORCEFRAME is set
  299. ;;      3. INCBP is set and proc is far
  300. ;;      4. LOADDS is set
  301. ;;
  302. ;; Force a prolog?      
  303.  
  304. ?doPrologue = ?doPrologue OR ?incbp OR ?loadds OR ?checkstack OR (cbLocals NE 0) OR (cbParams NE 0)
  305.  
  306. if      ?doPrologue EQ 0        ;; No epilog needed -- so get out of here
  307.     ret
  308.     exitm
  309. endif
  310.  
  311. ifnb    rgRegs                  ;; Pop off the registers -- they are in
  312.     for r,rgRegs            ;; inverse order from the prologue call
  313.         pop     r
  314.     endm
  315. endif
  316.  
  317. if      ?loadds                 ;;
  318.     dec     bp
  319.     dec     bp
  320.     mov     sp,bp
  321.     pop     ds
  322.     pop     bp
  323. else
  324.  
  325.     mov     sp,bp
  326.     pop     bp
  327. endif
  328.  
  329. if      ?incbp                  ;; Remove the increment of BP if necessary
  330.     dec     bp
  331. endif
  332.  
  333. if      flags AND 010h          ;; Caller pops stack arguments
  334.     ret
  335. else                            ;; Callee pops args
  336.  if     cbParams NE 0           ;; Put out the correct form of return
  337.     ret     cbParams
  338.  else
  339.     ret
  340.  endif
  341. endif
  342. endm
  343.  
  344. popcontext      listing
  345. .listmacro
  346.